home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / overm82.zip / MSG2TXT.ZIP / MSG2TXT.C < prev    next >
C/C++ Source or Header  |  1990-02-24  |  7KB  |  273 lines

  1. /* MSG2TXT.C
  2.  *------------------------------------------------------------------------
  3.  *
  4.  *  Program to convert a RBBS Messages file to text format
  5.  *
  6.  *   Tom Collins
  7.  *   01-20-90
  8.  */
  9.  
  10. #pragma pack(1)
  11.  
  12. typedef unsigned int Bit;
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <io.h>
  18. #include <stdarg.h>
  19. #include <fcntl.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22. #include <dos.h>
  23. #include "rbbs.h"
  24.  
  25. #define  TRUE     1
  26. #define  FALSE    0
  27. #define  RBBS_EOL '\xE3'
  28. #define  DELETED_MESSAGE '\xE2'
  29.  
  30. char *trim_string(char *);
  31. void txt_printf(FILE *,int,char *,...);
  32.  
  33. int lines_printed;
  34. int use_page_feeds;
  35.  
  36. void main(int argc,char **argv)
  37. {
  38.    int    fhin;
  39.    FILE   *fpout;
  40.    char   *full_txt, *txt, *ptr;
  41.    int    txt_size;
  42.    int    first_rec, last_rec, next_avail_rec;
  43.    int    recs_in_msg;
  44.    int    message_number, starting_message_number;
  45.    int    anything_exported;
  46.    int    no_privates;
  47.    int    i, j;
  48.    int    msgs_processed, recs_processed;
  49.    int    recs_to_process;
  50.    struct rbbs_checkpoint     chkpt;
  51.    struct rbbs_message_header hdr;
  52.    struct dosdate_t ddate;
  53.    struct dostime_t dtime;
  54.  
  55.    if (argc < 3)
  56.       {
  57.       printf("Usage: MSG2TXT Messages_File_Name Text_File_Name [Start] [/NOPAGE] [/NOPRIVATE]\n\n");
  58.       printf("       Messages_File_Name    = Name of your RBBS messages file\n");
  59.       printf("       Text_File_Name        = Name of the file to write the text to\n");
  60.       printf("       Start (Optional)      = Start exporting at what message number\n");
  61.       printf("       /NOPAGE (Optional)    = Don't put page feeds in the output file\n");
  62.       printf("       /NOPRIVATE (Optional) = Don't export private messages\n");
  63.       exit(1);
  64.       }
  65.    printf("MSG2TXT v0.02C 01-20-90 - Super Dooper RBBS Message to Text Converter\n\n");
  66.  
  67.    printf("Input File : %s\n",strupr(argv[1]));
  68.    printf("Output File: %s\n\n",strupr(argv[2]));
  69.  
  70.    starting_message_number = 0;
  71.    use_page_feeds = TRUE;
  72.    no_privates = FALSE;
  73.    anything_exported = FALSE;
  74.  
  75.    if (argc > 3)
  76.       {
  77.       starting_message_number = atoi(argv[3]);
  78.       for (i = 3; i < argc; i++)
  79.      {
  80.      if (strcmp(strupr(argv[i]),"/NOPAGE") == 0)
  81.         {
  82.         use_page_feeds = FALSE;
  83.         }
  84.      if (strcmp(strupr(argv[i]),"/NOPRIVATE") == 0)
  85.         {
  86.         no_privates = TRUE;
  87.         }
  88.      }
  89.       }
  90.  
  91.    fhin = open(argv[1],O_RDONLY|O_BINARY);
  92.    if (fhin < 0)
  93.       {
  94.       printf("Error Opening Messages File %s - Program Aborted\n",argv[1]);
  95.       exit(1);
  96.       }
  97.    fpout = fopen(argv[2],"wt");
  98.    if (fpout == NULL)
  99.       {
  100.       printf("Error Opening Text File %s - Program Aborted\n",argv[2]);
  101.       exit(1);
  102.       }
  103.    read(fhin,(char *)&chkpt,sizeof(chkpt));
  104.    first_rec      = atoi(chkpt.first_message);
  105.    last_rec       = atoi(chkpt.last_message);
  106.    next_avail_rec = atoi(chkpt.next_message);
  107.  
  108.    i = first_rec;
  109.    msgs_processed = recs_processed = lines_printed = 0;
  110.    recs_to_process = next_avail_rec - first_rec;
  111.  
  112.    printf("Records to Process: %i\n",recs_to_process);
  113.    if (use_page_feeds)
  114.       {
  115.       txt_printf(fpout,0,"\f");
  116.       }
  117.    txt_printf(fpout,1,"MSG2TXT Conversion: %s -> %s",argv[1],argv[2]);
  118.  
  119.    _dos_getdate(&ddate);
  120.    _dos_gettime(&dtime);
  121.    txt_printf(fpout,1,"Converted: %02u-%02u-%4u %02u:%02u:%02u",
  122.               ddate.month, ddate.day, ddate.year,
  123.               dtime.hour, dtime.minute, dtime.second);
  124.  
  125.    if (starting_message_number != 0)
  126.       {
  127.       txt_printf(fpout,3,"Beginning Export at Message # %i",
  128.          starting_message_number);
  129.       }
  130.    else
  131.       txt_printf(fpout,2,"");
  132.  
  133.    lseek(fhin,((long)(first_rec-1)*128L),SEEK_SET);
  134.    do {
  135.       read(fhin,(char *)&hdr,sizeof(hdr));
  136.       recs_in_msg = atoi(hdr.message_rec);
  137.       if (recs_in_msg <= 0)
  138.          {
  139.          recs_in_msg = 1;
  140.          }
  141.       message_number = atoi(hdr.mess_number);
  142.  
  143.       if (message_number < starting_message_number ||
  144.           (hdr.read_only == '*' && no_privates)    ||
  145.           hdr.active_indicator == DELETED_MESSAGE)
  146.          {
  147.      lseek(fhin,(long)((recs_in_msg-1)*128),SEEK_CUR);
  148.          }
  149.       else
  150.          {
  151.          txt_printf(fpout,1," Msg #: %4.4s",&(hdr.read_only));
  152.          txt_printf(fpout,1,"  From:  %31.31s Sent:  %8.8s %5.5s",hdr.message_from,hdr.message_date_sent,hdr.message_time);
  153.          txt_printf(fpout,1,"    To:  %22.22s          Rcvd:  -NO-",hdr.message_to);
  154.          txt_printf(fpout,2,"  Subj:  %25.25s",hdr.message_subject);
  155.  
  156.          txt_size = (recs_in_msg-1)*128;
  157.      full_txt = malloc(txt_size + 1);
  158.  
  159.      read(fhin,full_txt,txt_size);
  160.      full_txt[txt_size] = '\0';
  161.  
  162.          txt = full_txt;
  163.          for (j = 0; j < txt_size; j++)
  164.             {
  165.             if (full_txt[j] == RBBS_EOL)
  166.                {
  167.                full_txt[j] = '\0';
  168.            trim_string(txt);
  169.            if (txt[0] != 0x01 && strstr(txt,"SEEN-BY") != txt)
  170.                   {
  171.                   txt_printf(fpout,1,"%s",txt);
  172.                   }
  173.                txt = &full_txt[j+1];
  174.                }
  175.             }
  176.      txt_printf(fpout,1,"");
  177.      free(full_txt);
  178.  
  179.      anything_exported = TRUE;
  180.      }
  181.  
  182.       i += recs_in_msg;
  183.       msgs_processed++;
  184.       recs_processed += recs_in_msg;
  185.  
  186.       printf(" %i Msgs  %i Recs\r",msgs_processed,recs_processed);
  187.       }
  188.    while (i < next_avail_rec);
  189.  
  190.    if (!anything_exported)
  191.       {
  192.       txt_printf(fpout,1,"No New Messages Found...");
  193.       }
  194.  
  195.    printf("Done Processing              \n");
  196.    fclose(fpout);
  197.    close(fhin);
  198.  
  199.    exit(0);
  200. }
  201.  
  202. /*  TRIM_STRING
  203.  *----------------------------------------------------------------------------
  204.  *
  205.  *  Removes trailing spaces and '\n's
  206.  *  from a string.
  207.  *
  208.  *  Globals Used:
  209.  *     <none>
  210.  *
  211.  *  Returns:
  212.  *     The trimmed string
  213.  */
  214.  
  215. char *trim_string(char *s)
  216. {
  217.    int i;
  218.  
  219. /*
  220.    i = 0;
  221.    while (s[i] != '\0')
  222.       if (s[i] == ' ')
  223.          i++;
  224.       else
  225.          break;
  226.    strcpy(s,&s[i]);
  227. */
  228.    for (i = strlen(s)-1; i >= 0; i--)
  229.       if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r')
  230.          s[i] = '\0';
  231.       else
  232.          break;
  233.  
  234.    return(s);
  235. }
  236.  
  237. void txt_printf( FILE *fp, int returns, char *p, ...)
  238. {
  239.     int i;
  240.     va_list arg_ptr;
  241.     char *buf;
  242.  
  243.     /*
  244.      *  Initalize
  245.      */
  246.  
  247.     buf = malloc(8192);
  248.  
  249.     /*
  250.      *  Process input string and optional parameters
  251.      */
  252.     va_start( arg_ptr, p);
  253.     i = vsprintf( buf, p, arg_ptr);
  254.  
  255.     fputs( buf, fp);
  256.     for (i = 0; i < returns; i++)
  257.        {
  258.        fputs("\n",fp);
  259.        lines_printed++;
  260.        if (lines_printed == 60 && use_page_feeds)
  261.           {
  262.           fputs("\f\n\n\n",fp);
  263.           lines_printed = 0;
  264.           }
  265.        }
  266.  
  267.     /*
  268.      *  Cleanup
  269.      */
  270.     free( buf);
  271. }
  272.  
  273.